package com.zhsj.OkeHomeConsumption.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.empty.IndexCityBean;
import com.zhsj.OkeHomeConsumption.other.OnReItemClickListener;

import java.util.List;


/**
 * @描述: 城市列表适配器
 * @创建人：zhangpeisen
 * @创建时间：2017/6/17 下午1:24
 * @修改人：zhangpeisen
 * @修改时间：2017/6/17 下午1:24
 * @修改备注：
 * @throws
 */
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {
    protected Context mContext;
    protected List<IndexCityBean> mDatas;
    protected LayoutInflater mInflater;

    /**
     * @Fields mSharedPreferanceUtils : TODO
     */

    private OnReItemClickListener mOnItemClickListener;

    public CityAdapter(Context mContext, List<IndexCityBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<IndexCityBean> getDatas() {
        return mDatas;
    }

    public CityAdapter setDatas(List<IndexCityBean> datas) {
        mDatas = datas;
        return this;
    }

    public CityAdapter setOnItemClickListener(OnReItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
        return this;
    }

    @Override
    public CityAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.selectcitylist_item_fragment, parent, false));
    }

    @Override
    public void onBindViewHolder(final CityAdapter.ViewHolder holder, final int position) {
        final IndexCityBean IndexCityBean = mDatas.get(position);
        holder.item_city_name.setText(IndexCityBean.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getPosition(holder);
                mOnItemClickListener.onItemClick(v, (IndexCityBean) mDatas.get(position), position);
            }
        });

    }

    protected int getPosition(RecyclerView.ViewHolder viewHolder) {
        return viewHolder.getAdapterPosition();
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView item_city_name;

        public ViewHolder(View itemView) {
            super(itemView);
            item_city_name = (TextView) itemView.findViewById(R.id.item_city_name);
        }
    }
}
